package bintree.heap.compare;

import java.util.Arrays;
import java.util.Comparator;

// 此时这个类是根据年龄越大就越大进行比较
class StudentSec implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}

// 此时又是根据年龄越小反而越大进行比较
class StudentDesc implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o2.getAge() - o1.getAge();
    }
}

// 显平需要一个按照name进行排序的比较，再创建一个新的类，实现compare方法即可



public class Student {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public static void main(String[] args) {
        Student[] students = new Student[]{
                new Student(19, "凯哥"),
                new Student(18, "铭哥"),
                new Student(20, "祥龙")
        };
        // 得到一个"升序"数组
        // Student这个类不具备可比较的能力，传入一个该类的比较器对象
        Arrays.sort(students, new StudentDesc());
        System.out.println(Arrays.toString(students));
    }
}